/**
 * 
 */
package com.hww.ArrayListAndLinkedList;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author:Huangwenwei
 * @date:2014-6-11
 * @time:下午8:52:25
 */
public class ListIteration {

	private static List<String> arrayList = new ArrayList<>();
	private static List<String> linkedList = new LinkedList<>();

	public static void initializeList(List<String> list, int bufferSize) {
		for (int i = 0; i < 50000; i++) {
			byte[] buffer = null;
			if (bufferSize > 0) {
				buffer = new byte[bufferSize];
			}
			String s = String.valueOf(i);
			list.add(s);

			// avoid buffer to be optimized away
			if (System.currentTimeMillis() == 0) {
				System.out.println(buffer);
			}
		}
	}

	public static void bench(List<String> list) {
		if (list.contains("bar")) {
			System.out.println("bar found");
		}
	}

	/**
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		if (args.length != 2)
			return;
		List<String> benchList = "array".equals(args[0]) ? arrayList
				: linkedList;
		int bufferSize = Integer.parseInt(args[1]);
		initializeList(benchList, bufferSize);
		HWCounters.init();
		System.out.println("init donw");

		// warmup
		for (int i = 0; i < 10000; i++) {
			bench(benchList);
		}

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("warmup done");

		HWCounters.start();

		for (int i = 0; i < 1000; i++) {
			bench(benchList);
		}
		HWCounters.stop();
		HWCounters.printResults();
		HWCounters.shutdown();

	}

}
